<?php

class DashboardController extends Controller {

    public function actionIndex() {
        $msg = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : null;
        if (Yii::app()->user->getId() !== null) {
            $this->checkRoleAndredirect($msg);
        } else {
            $this->actionLogin();
        }
    }

    private function checkRoleAndredirect($msg) {
        $roles = Rights::getAssignedRoles(Yii::app()->user->Id);
        $flag = FALSE;
        foreach ($roles as $role) {
            if ($role->name == 'Admin') {
               $grf= $this->getBookinggraph();
                $this->render('admin',array('graph1'=>$grf));
                $flag = TRUE;
                break;
            } elseif ($role->name == 'employee') {
                $cal_config = array();
                $cal_config['defaultView'] = 'agendaDay';
                $cal_config['minTime'] = Conf::getConf('DAY START');
                $cal_config['maxTime'] = Conf::getConf('DAY END');
                $cal_config['slotMinutes'] = 5;
                $cal_config['selectable'] = true;
                $cal_config ['selectHelper'] = true;
                $cal_config['editable'] = true;
                $cal_config['selectable'] = false;
                CJSON::encode($cal_config);
                $this->render('employee', array('cal_config' => CJSON::encode($cal_config)));
                $flag = TRUE;
                break;
            } elseif ($role->name == 'client') {
                $criteria = new CDbCriteria();
                $criteria->condition = "userid=" . Yii::app()->user->id;
                $count = ServiceRequest::model()->count($criteria);
                $pages = new CPagination($count);
                $pages->pageSize = 10;
                $pages->applyLimit($criteria);
                $requests = ServiceRequest::model()->findAll($criteria);
                $this->render('client', array('requests' => $requests, 'pages' => $pages, 'msg' => $msg));
                $flag = TRUE;
                break;
            }
        }
        if (!$flag) {
            $this->render('index');
        }
    }

    public function actionLogin() {
        $model = new LoginForm;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        // display the login form
        $this->render('login', array('model' => $model));
    }
    
    public function getBookinggraph(){
         $date = date("Y-m-d");
        $q="select eventOn, count('eventOn') as daycount from resource_timing where eventOn between  '".$date."' and  '2014-03-03' GROUP BY eventOn";
       $results= Yii::app()->db->createCommand($q)->queryAll();
       if (isset($results)){
           $f=FALSE;
           $grph="[";
           foreach ($results as $key => $result) {
               if($f){
                    $grph.=",";
               }else{
                  $f=TRUE; 
               }
               $grph.="['".$result['eventOn']."',".$result['daycount']."]";
           }
           $grph.="]";
           return $grph;
       }else{
           return"";
       }
        
    }

    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }

      public function actions()
      {
      // return external action classes, e.g.:
      return array(
      'action1'=>'path.to.ActionClass',
      'action2'=>array(
      'class'=>'path.to.AnotherActionClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */
}
